/******* SERVEUR TCP **************/
#include "connection_socket.h"
#include <stdio.h>
#include "sqlite_source/sqlite3.h"

bool chat_connect(requete_socket *request, char* addr_ip, connection_socket* conn);

int main(int argc, char* argv[])
{
	connection_socket* conn = new connection_socket();
	conn->debug_mode = true;
	conn->connect_socket();

	while(conn->accept_socket()){
        if(!fork()){
            printf("Réception d'un socket\n");
            requete_socket* requete = conn->recv_socket();
            switch(requete->type){
                case SOCKET_CONNECT:
                    chat_connect(requete, inet_ntoa (conn->addr.sin_addr),conn);
                    break;
            }
            exit(0);
        }
    }
    conn->close_socket();
}

bool chat_connect(requete_socket *request, char* addr_ip, connection_socket* conn){
    sqlite3 *db;
   int rc;

   rc = sqlite3_open("backup.db", &db);

   if( rc ){
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }else{
        printf("Opened database successfully\n");
    }
    /* Create SQL statement */
    char *sql = sqlite3_mprintf("SELECT * from tab_utilisateur where login='%q'", request->login);
    printf("%s\n",sql);
    const char* data = "Callback function called";
    char *zErrMsg = 0;
    /* Execute SQL statement */
    sqlite3_stmt* query = NULL;
    rc = sqlite3_prepare(db, sql, -1, &query, NULL);
    if( rc != SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
    }else{
        fprintf(stdout, "Operation done successfully\n");
    }
    int result = sqlite3_step(query);
    printf("%d resutat \n",result);
    if(SQLITE_ROW == result){
        char sql_update[SIZE_DATA];
        sprintf(sql_update, "UPDATE tab_utilisateur set ip='%s' where login='%s'",addr_ip,request->login);
        printf("%s \n",sql_update);
        sqlite3_exec(db,sql_update,0,0, &zErrMsg);
    }
    else{
        char sql_update[SIZE_DATA];
        sprintf(sql_update, "INSERT INTO tab_utilisateur (login, ip) values ('%s','%s')",request->login, addr_ip);
        printf("%s \n",sql_update);
        sqlite3_exec(db,sql_update,0,0, &zErrMsg);
    }
    char *sql_friend;
    sprintf(sql_friend, "SELECT login FROM tab_utilisateur where login != '%s'",request->login);
    char *list_user = new char[SIZE_DATA];
    for(int i = 0; i<strlen(list_user);i++){
        list_user[i] = 0;
    }
    rc = sqlite3_prepare(db, sql_friend, -1, &query, NULL);
    if( rc != SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        return false;
    }else{
        fprintf(stdout, "Operation done successfully\n");
    }
    while(SQLITE_DONE!=sqlite3_step(query)){
        char *login_friend = (char *) sqlite3_column_text(query,0);
        if(strlen(list_user)==0){
            sprintf(list_user, "%s",login_friend);
        }
        else{
            sprintf(list_user, "%s;%s",list_user,login_friend);
        }
    }
    requete_socket* requete_friend = new requete_socket();
    strcpy(requete_friend->login, "SERVEUR");
    strcpy(requete_friend->data, list_user);
    strcpy(requete_friend->dest, addr_ip);
    conn->send_socket(requete_friend);
    sqlite3_close(db);
    return true;
}
